package controlLayer;
import modelLayer.*;
import java.util.*;

public class OrderCtr {
    
    private OrderContainer orderContainer;
    private CustomerCtr custCtr;
    private EmployeeCtr empCtr;
    private ProductCtr prodCtr;
    private Customer cust;
    private Employee emp;
    private ArrayList<OrderLine> orderLines;
    private double subtotal;
    private double total;
    
    public OrderCtr() {
        orderContainer = OrderContainer.getInstance();
        custCtr = new CustomerCtr();
        empCtr = new EmployeeCtr();
        prodCtr = new ProductCtr();
    }
    
    /**
     * Order methods
     */
    public void createOrder(int ccpr, int ecpr) {
        orderLines = new ArrayList<>();
        cust = custCtr.findCustomer(ccpr);
        emp = empCtr.findEmployee(ecpr);
        addProductsToOrder(); 
    }
    
    private void addProductsToOrder() {
        Scanner keyboard = new Scanner(System.in);
        int id = inputBarcode();
        Product prod = prodCtr.findProduct(id);
        if(prod != null) {
            int quantity = inputQuantity();
            if(quantity <= prod.getInStock()) {
                OrderLine ol = prodCtr.createOrderLine(prod, quantity);
                subtotal = prod.getPrice() * quantity;
                orderLines.add(ol);
                
                System.out.println();
                System.out.println("1.Add more products");
                System.out.println("0.Create order");
                System.out.println();
                int choice = keyboard.nextInt();
                boolean stop = false;
                
                while(!stop) {
                    stop = true;
                    
                    switch(choice) {
                    case 1 : total += subtotal;
                             addProductsToOrder();
                             prod.setInStock(prod.getInStock()-quantity);
                        break;
                    case 0 : String date = inputDate();
                            total += subtotal;
                            prod.setInStock(prod.getInStock()-quantity);
                            Order ord = new Order(cust, emp, orderLines, date, total);
                            OrderContainer.getInstance().addOrder(ord); 
                            subtotal = 0;
                            total = 0;
                           
                            System.out.println("Order " + ord.getId() + " has been succesfully created");
                        break;
                    }
                }
            }
            else {
                System.out.println("The quantity selected is not available in stock");
            }
        }
        else {
            System.out.println("No product was found with the specified barcode");
        }
    }
    
    public Order findOrder(int id) {
        Order ord = orderContainer.findOrder(id);
        return ord;
    }
    
    public int inputBarcode() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the barcode of the product (numeric value) : ");
        int barcode = keyboard.nextInt();
        return barcode;
    }
    
    public int inputQuantity() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the quantity : ");
        int quantity = keyboard.nextInt();
        return quantity;
    }
    
    private String inputDate() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the order date (dd/mm/yyyy) : ");
        String date = keyboard.nextLine();
        return date;
    }
    
    /**
     * Find employee & customer methods
     */
    
    public Customer findCustomer(int ccpr) {
        cust = custCtr.findCustomer(ccpr);
        return cust;
    }
    
    public Employee findEmployee(int ecpr) {
        emp = empCtr.findEmployee(ecpr);
        return emp;
    }
}
